﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace FarmacieDAL
{
    public class RetetaManager
    {
        private static List<Reteta> toateRetetele;
        private static List<Detalii_Reteta> detaliiRetete;

        private static FarmacieEntities entities;

        public static void Initialize(FarmacieEntities entitiesContext)
        {
            toateRetetele = new List<Reteta>();
            detaliiRetete = new List<Detalii_Reteta>();

            entities = entitiesContext;

            toateRetetele = entities.Reteta.ToList();
            detaliiRetete = entities.Detalii_Reteta.ToList();
        }

        public static void Refresh()
        {
            toateRetetele.Clear();
            detaliiRetete.Clear();

            entities.Refresh(RefreshMode.StoreWins, entities.Reteta);
            entities.Refresh(RefreshMode.StoreWins, entities.Detalii_Reteta);

            toateRetetele.AddRange(entities.Reteta);
            detaliiRetete = entities.Detalii_Reteta.ToList();
        }

        public static List<Substanta_Activa> GetSubstanteActiveByReteta(Reteta reteta)
        {
            List<Substanta_Activa> result = new List<Substanta_Activa>();

            foreach (Detalii_Reteta detaliiReteta in detaliiRetete)
                if (detaliiReteta.Id_Reteta.Equals(reteta.Id_Reteta))
                    result.Add(detaliiReteta.Substanta_Activa);
            
            return result;
        }

        public static List<Reteta> ToateRetetele
        {
            get
            {
                return toateRetetele;
            }
        }
    }
}
